﻿#region License

// Copyright (c) 2009, Bill O'Neill (william.w.oneill@gmail.com)
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
// 1. Redistributions of source code must retain the above copyright 
// notice, this list of conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright 
// notice, this list of conditions and the following disclaimer in the 
// documentation and/or other materials provided with the distribution.
// 
// 3. Neither the name of Focal Point Technology Solutions, LLC nor 
// the names of its contributors may be used to endorse or promote 
// products derived from this software without specific prior written 
// permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
// DAMAGE.

#endregion

using System;

namespace X10d.Interface.States
{
	/// <summary>
	/// Represents the FSA state that sends the function to the X10 interface
	/// </summary>
	internal class SendFunctionState : PortStateBase
	{
		/// <summary>
		/// Sends the command to the interface
		/// </summary>
		/// <param name="requestManager">
		/// Reference to the request manager
		/// </param>
		public override void HandleArrive(IRequestManager requestManager)
		{
			const int MAX_X10_DIM_VALUE = 22;

			byte[] buffer = new byte[2];

			// Build the "dim" high word
			if (requestManager.CurrentCommand.Function == FunctionCodeType.Bright ||
			    requestManager.CurrentCommand.Function == FunctionCodeType.Dim)
			{
				if (!requestManager.CurrentCommand.LightLevel.HasValue)
				{
					throw new InvalidOperationException(
						"A dim command was sent to the device but the dim level was not set.");
				}

				// Convert the percentage to an X10 dim value (0-22)
				int lightLevel = (MAX_X10_DIM_VALUE*requestManager.CurrentCommand.LightLevel.Value/DeviceCommand.MaxDimValue);
				lightLevel <<= 2;
				buffer[0] = (byte) lightLevel;
			}

			// Send the function
			Log.Info("Sending function");
			buffer[0] |= Function;
			buffer[1] = (byte) ((byte) requestManager.CurrentCommand.HouseCode | (byte) requestManager.CurrentCommand.Function);
			Log.WriteBuffer(buffer);

			requestManager.Port.Write(buffer, 0, buffer.Length);

			requestManager.TransitionToState( new VerifyChecksumState<SendReadyState<IdleState>>( buffer ) );
		}

		/// <summary>
		/// The state object cannot handle data. The request manager should have been migrated
		/// to a different state to handle inbound data.
		/// </summary>
		/// <param name="requestManager">
		/// Reference to the request manager
		/// </param>
		/// <param name="newBuffer">
		/// New data from the serial port
		/// </param>
		protected override void DoHandleNewBuffer(IRequestManager requestManager, byte[] newBuffer)
		{
			requestManager.TransitionToState( new ErrorState() );
		}
	}
}